package com.example.statemachine.util;

import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;

/**
 * @author jianwei.wang
 * @version v1.0
 * @date 2019/5/15 18:08
 **/
@Slf4j
@ControllerAdvice
public class ExceptionAdvice {

    @ExceptionHandler(value = BusinessException.class)
    @ResponseStatus(HttpStatus.OK)
    @ResponseBody
    public MessageBody handleCustomException(BusinessException e){
        log.error("业务操作异常，errMsg={},stack info={}"+e.getMessage(),e);
        return e.getCode()==null?MessageBody.error(e.getMessage()):MessageBody.error(e);
    }

    @ExceptionHandler(value = Exception.class)
    @ResponseStatus(HttpStatus.OK)
    @ResponseBody
    public MessageBody handleException(Exception e){
        log.error("系统操作异常，errMsg={},stack info={}"+e.getMessage(),e);
        return MessageBody.error("系统操作异常，请稍后重试！");
    }

}
